// subpages/pages/cancelNFCBind/cancelNFCBind.js
//获取应用实例
const app = getApp()
const util = require('../../../utils/util.js')
Page({

    /**a
     * 页面的初始数据
     */
    data: {
        loadding : 0,
        checkpointType:[],
        checkpointTypeIndex:-1,
        addressIndex:-1,
        selectVisble:false,
        pointTypeName:"点击此处选择",
        addressName:"点击此处选择",
        address:[],
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        let checkpointType=[]
        checkpointType.push({
          type: 2,
          item:"巡检",
          checked: false
        })
        checkpointType.push({
          type: 3,
          item:"值周",
          checked: false
        })
        this.setData({
          checkpointType:checkpointType
        })
    },
     showModal3(e) {
    let checkpointType = this.data.checkpointType.map((item, index) => {
      return {
        type:item.type,
        item: item.item,
        checked: index == this.data. checkpointTypeIndex ? true : false
      }
    })
    this.setData({
      modalName3: "RadioModal",
      checkpointType: checkpointType,
      modalComfirm3: this.data.selectedIndex3 != null ? true : false,
      // forwardId: e.currentTarget.dataset.id
    })
  },
  showModal2(e) {
    if(this.data.checkpointTypeIndex != -1&&this.data.address.length!=0){
    this.setData({
        modalName2: "RadioModal",
        modalComfirm2: this.data.selectedIndex2 != null ? true : false,
        // forwardId: e.currentTarget.dataset.id
      })
    }else if(this.data.address.length==0){
        wx.showToast({
            title: '请重选设备类别',
            image: "../../../images/error.png"
          })
    }
    else{
        wx.showToast({
          title: '设备类别未输入',
          image: "../../../images/error.png"
        })
      }
  },
  hideModal3(e) {
    this.setData({
      modalName3: null
    })
  },
  hideModal2(e) {
    this.setData({
      modalName2: null
    })
  },
  radioSelect3(e) {
    console.log("radioIndex3",e.currentTarget.dataset.index)
    let radioIndex3 = e.currentTarget.dataset.index,
    checkpointType = this.data.checkpointType.map((item, index) => {
        return {
          type:item.type,
          item: item.item,
          checked: index == radioIndex3 ? true : false
        }
      }),
      status
    this.setData({
      checkpointType: checkpointType,
      selectedIndex3: radioIndex3
    })
    if (this.data.selectedIndex3 != null) status = true
    else status = false
    this.setData({
      modalComfirm3: status
    })
  },
  radioSelect2(e) {
    console.log("radioIndex2",e.currentTarget.dataset.index)
    let radioIndex2 = e.currentTarget.dataset.index,
    address = this.data.address.map((item, index) => {
        return {
            address: item.address,
            macAddress:item.macAddress,
          checked: index == radioIndex2 ? true : false
        }
      }),
      status
    this.setData({
        address: address,
      selectedIndex2: radioIndex2
    })
    if (this.data.selectedIndex2 != null) status = true
    else status = false
    this.setData({
      modalComfirm2: status
    })
  },
  confirmSelect2(e) {
    this.setData({
      addressIndex: this.data.selectedIndex2,
      modalName2: null
    })},
  confirmSelect3(e) {
    this.setData({
      checkpointTypeIndex: this.data.selectedIndex3,
      modalName3: null
    })
    console.log("index"+this.data.checkpointType[this.data.checkpointTypeIndex].item);
    // if(this.data.checkpointType[this.data.checkpointTypeIndex].type==2){
    //   this.setData({

    //     selectVisble:true,
    //   })
    // }
    // else if(this.data.checkpointType[this.data.checkpointTypeIndex].type==3){
    //   this.setData({

    //     selectVisble:false,
    //   })
    // }
    wx.request({
        url: app.inspectUrl + '/WeChat/weChatSystem/getAllNFCdevice',
        method: 'POST',
        dataType: 'json',
        data: {
          schoolId : parseInt(app.globalData.departmentId) ,
          type : this.data.checkpointType[this.data.checkpointTypeIndex].type
        },
        success: res => {
          
          let address=res.data.map((item, index)=>{
            return {
                address: item.address,
                macAddress:item.macAddress,
                checked: false
              }
          })
          this.setData({
              address:address
          })
          if(this.data.address.length==0){
            this.setData({
                addressName:"无对应设备，请重新选择"
            })
          }else{
            this.setData({
            addressName:"点击此处选择",
        })
          }
          console.log("address", this.data.address.length)

        }
      })
  },
  formSubmit: util.throttle(function(e) {
    
    let waringInfo = ""
    if (this.data.checkpointTypeIndex == -1 && this.data.pointTypeName == "点击此处选择") {
     waringInfo = "请选择打卡类别"
    } 
   else if (this.data.addressIndex == -1) {
     waringInfo = "请选择设备地点"
    } else{
     wx.showLoading({
       title: "正在解除绑定"
     })
     wx.request({
        url: app.inspectUrl + '/WeChat/weChatSystem/CancelNFCBind',
        method: 'POST',
        data: {
          schoolId: app.globalData.departmentId,
          address: this.data.address[this.data.addressIndex].address,
          macAddress: this.data.address[this.data.addressIndex].macAddress, 
          
         
        },
        dataType: 'json',
        success: res => {
          console.log("res",res)
          if(res.data == 'success'){
            wx.showToast({
              title: "解绑成功"
            })
            // wx.navigateTo({
            //   url: '../../../subpages/pages/nfcResponse/nfcResponse'
            // })
            
          }
        },
        fail: () => {
          wx.showToast({
            title: '网络异常！',
            mask: true,
            image: "/images/error.png"
          })
        }
      })
     console.log("schoolId", parseInt(app.globalData.departmentId));
     console.log("address", this.data.address[this.data.addressIndex].address);
     console.log("address", this.data.address[this.data.addressIndex].macAddress);
     this.setData({
        checkpointTypeIndex:-1,
        addressIndex:-1,
        selectVisble:false,
        pointTypeName:"点击此处选择",
        addressName:"点击此处选择",
        address:[],
     })
   }

   if (waringInfo != "") {
     wx.showToast({
       title: waringInfo,
       image: "/images/warning.png"
     })
   }
   
 }),

   
})